Skip to content

Migrate component unit tests#66

Merged
cap10morgan merged 5 commits intomainfrom
chore/unit-tests-third-times-a-charm
Jan 12, 2026
Merged

Migrate component unit tests#66
cap10morgan merged 5 commits intomainfrom
chore/unit-tests-third-times-a-charm

Conversation

@cap10morgan
Copy link
Member

@cap10morgan cap10morgan commented Dec 24, 2025

Supersedes #31. Hopefully third time's a charm!

This is still slightly a work in progress. I need to verify a few more things before merging, but it's coming together nicely and wanted to get it up for review and for my own re-orientation with it after the holiday break.

Should be ready for review now!

@cap10morgan cap10morgan force-pushed the chore/unit-tests-third-times-a-charm branch from bb277e9 to 635fd83 Compare December 24, 2025 00:24
@cap10morgan
Copy link
Member Author

Would love to hear if anyone has any insight into this integration test failure: https://github.com/HarperFast/harper/actions/runs/20474799099/job/58837312244?pr=66

@kriszyp
Copy link
Member

kriszyp commented Dec 24, 2025

Do you have more of an explanation for the strategies here?

  • Why are we changing the source code to using subpath patterns? Is this necessary for a unit test PR? Is it just for ergonomics?
  • How does a unit test specify to require the built CJS vs the source TypeScript module? It seems this is setup as a single global switch with a conditional, but I thought we had agreed that TypeStrip vs built was also a module decision, based on whether a unit test made modifications to the module record. (Ideally a unit test can specify using only dist/built code, or it can use the default module source, which is dependent on the global switch)
  • Currently, the PR doesn't migrate any unit tests harperdb, right? That is also intended to be part of this PR? That migration effort will involve a similar move to subpaths, based on the patterns established in the component tests?

@cap10morgan
Copy link
Member Author

Do you have more of an explanation for the strategies here?

  • Why are we changing the source code to using subpath patterns? Is this necessary for a unit test PR? Is it just for ergonomics?

I need to reassess this and write up what I'm seeing. More to come...

  • How does a unit test specify to require the built CJS vs the source TypeScript module? It seems this is setup as a single global switch with a conditional, but I thought we had agreed that TypeStrip vs built was also a module decision, based on whether a unit test made modifications to the module record. (Ideally a unit test can specify using only dist/built code, or it can use the default module source, which is dependent on the global switch)

There's a ton of flexibility here we could take more / different advantage of, but currently yes, it is a single global switch. We could add more import subpath patterns that simply point at ./dist/*.js regardless of any conditions being set (and similarly for TypeStrip paths). But this seemed like the simplest starting point for this PR, and keeping things simple (believe it or not!) was my primary goal for this iteration.

  • Currently, the PR doesn't migrate any unit tests harperdb, right? That is also intended to be part of this PR? That migration effort will involve a similar move to subpaths, based on the patterns established in the component tests?

It migrates the component unit tests, but it's perhaps a little hard to see since git views it as a renaming of the previously-merged component unit tests.

And yes, additional migrations will involve similar changes as this PR demonstrates. I plan to migrate one or two more categories of tests and then write up a migration guide for others to use to migrate more.

...with subpath import patterns
These don't all pass yet, but nice to have to find places we need to fix up.
@cap10morgan cap10morgan force-pushed the chore/unit-tests-third-times-a-charm branch from b0a57a2 to 75dc592 Compare January 2, 2026 18:03
@cap10morgan
Copy link
Member Author

Do you have more of an explanation for the strategies here?

  • Why are we changing the source code to using subpath patterns? Is this necessary for a unit test PR? Is it just for ergonomics?

I need to reassess this and write up what I'm seeing. More to come...

OK it looks like this is unnecessary now. Which is great! So this PR now excludes the commit that changed all of the source code requires / imports to use subpath import patterns. We still can (and IMO should) use them any/everywhere, but they now appear to be out of scope for this PR. Whatever I was running into before that was improved by changing them all seems to no longer be an issue.

@cap10morgan cap10morgan marked this pull request as ready for review January 2, 2026 18:12
@cap10morgan cap10morgan requested a review from a team as a code owner January 2, 2026 18:12
@cap10morgan
Copy link
Member Author

Would love to hear if anyone has any insight into this integration test failure: https://github.com/HarperFast/harper/actions/runs/20474799099/job/58837312244?pr=66

It went away! (🎉)

Copy link
Member

@kriszyp kriszyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the process look like for migrating tests? How much time does it take, or will take, to copy over tests? Is there a script or does AI heavily speed up this process? Will most over of tests copy over with this procedure? Or some?

reset();
});
const { describe, it, afterEach } = require('mocha');
const assert = require('node:assert/strict');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is directly recopying the test from /harperdb, eliminating any .ts conversion or conversion to ESM, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was added because it is nice to explicitly require mocha and import the test functions? But is not strictly necessary for migration?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, and because sometimes my IDE realizes these are globals, and sometimes it doesn't. And because I'm allergic to globals. 😝

assert.equal(status.message, 'Connection lost');
});

it('should report healthy status', function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also code cleanup? But not necessary, and we have to be careful not to do this if this is accessed (setting this.timeout for example, or using this to hold some state)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and an attempt to make the style more consistent. But yes, if the test needs access to this, then it has to use function () syntax. I've had good luck with eliminating those accesses so far, though. :)

const UNIT_TEST_DIR = __dirname;
const ENV_DIR_NAME = 'testEnv';
const ENV_DIR_PATH = path.join(UNIT_TEST_DIR, ENV_DIR_NAME);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we haven't copied some functions, like cleanUpDirectories that are in use by other tests that will be migrated? But we have a plan to replace?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I only wanted to migrate fns that are actually used by tests in this repo. So if there are other testUtils fns needed by tests we migrate down the road, I think we should bring in those fns in the corresponding migration PRs.

@cap10morgan
Copy link
Member Author

What does the process look like for migrating tests? How much time does it take, or will take, to copy over tests? Is there a script or does AI heavily speed up this process? Will most over of tests copy over with this procedure? Or some?

All good questions, but I don't think we'll know the answers with too much certainty until we migrate a few more. If I had to make an educated guess, I would say the more recently written tests might take part of a day to migrate (an entire dir / category of), and the older tests will take significantly longer / essentially need to be rewritten.

But the basic starting point is to copy a directory of tests from harperdb, update the requires to use the import subpath patterns, and then try running them. I have commonly run into sandboxing issues at that point with earlier test migrations (where the tests cause / rely on leakage into / out of the ~/hdb dir). The new createTestSandbox fn in unitTests/testUtils.js seems to take care of most or all of those issues that I've encountered.

But of course things could always be different on this branch. Hopefully in ways that I can borrow fixes from previous attempts.

@kriszyp
Copy link
Member

kriszyp commented Jan 5, 2026

It seems like "migrate" keeps encompassing an effort to change code (globals -> require, function -> arrow function, etc.) prior to bringing it into harper and merging it. This seems contradictory to our goals and values of trying to do as much in the open and public as possible. Why do we need to do this before merging and obscuring these changes from the public? And don't we benefit from tracking any changes to tests with clear issue tracking as well? (why not have issues for changing functions, globals, etc.?)

the older tests will take significantly longer / essentially need to be rewritten.

Why will these need to be rewritten (in the initial migration)? I thought that by using the dist directory, we should be able to modify module records and preserve things like rewrite and stubbing?

@cap10morgan
Copy link
Member Author

It seems like "migrate" keeps encompassing an effort to change code (globals -> require, function -> arrow function, etc.) prior to bringing it into harper and merging it. This seems contradictory to our goals and values of trying to do as much in the open and public as possible. Why do we need to do this before merging and obscuring these changes from the public? And don't we benefit from tracking any changes to tests with clear issue tracking as well? (why not have issues for changing functions, globals, etc.?)

There's always a balance between the core goal and opportunistic, quick, easy cleanups you find along the way. Ideally these "side quest" changes are things that are non-controversial improvements that don't distract from the main goal and are typically low-priority changes that have a hard time getting addressed as a primary goal. If / when they stray too far from that ideal for whatever reason, I'm happy to revert them and save them for a future PR.

the older tests will take significantly longer / essentially need to be rewritten.

Why will these need to be rewritten (in the initial migration)? I thought that by using the dist directory, we should be able to modify module records and preserve things like rewrite and stubbing?

I'm just speculating. Perhaps they won't need that!

@kriszyp
Copy link
Member

kriszyp commented Jan 5, 2026

opportunistic, quick, easy cleanups

Yes, but it has been almost 3 months since we opened started this repo, and I think we have less than 10% of unit tests migrated, so it feels disingenuous to me to be calling any of this "quick"/"easy". Currently any focused development on this repo is significantly hindered and endangered by the lack of unit tests, and at this point seems pretty hard to justify unit test cleanup getting in the way of simply getting the tests in place and running.

along the way

I am skeptical that before committing to this repo it is "along the way", but once committed, it really is out of the way.

I'm just speculating. Perhaps they won't need that!

It seems like focusing on getting as many tests migrated and identifying issues with that should be the priority.
And it is definitely possible/likely that some of these tests could be deleted. But I don't think that necessarily corresponds with new vs old tests. There are old tests that still provide significant value, that we do not have the resource/bandwidth to try to rewrite in the v5 timeline.

@cap10morgan
Copy link
Member Author

opportunistic, quick, easy cleanups

Yes, but it has been almost 3 months since we opened started this repo, and I think we have less than 10% of unit tests migrated, so it feels disingenuous to me to be calling any of this "quick"/"easy". Currently any focused development on this repo is significantly hindered and endangered by the lack of unit tests, and at this point seems pretty hard to justify unit test cleanup getting in the way of simply getting the tests in place and running.

A lot of this was done much earlier in the process. And like I said, I'm happy to revert any / all of it. We're on the same page w/r/t priorities.

along the way

I am skeptical that before committing to this repo it is "along the way", but once committed, it really is out of the way.

Not sure I follow, but like I said, I'm happy to revert any / all of those changes.

I'm just speculating. Perhaps they won't need that!

It seems like focusing on getting as many tests migrated and identifying issues with that should be the priority. And it is definitely possible/likely that some of these tests could be deleted. But I don't think that necessarily corresponds with new vs old tests. There are old tests that still provide significant value, that we do not have the resource/bandwidth to try to rewrite in the v5 timeline.

I'm certainly not proposing we delete any tests. We're on the same page regarding priorities.

@cap10morgan
Copy link
Member Author

@kriszyp Does 630b499 look better?

Copy link
Member

@kriszyp kriszyp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kriszyp Does 630b499 look better?

Yes. I really wasn't trying to get you to revert things either, honestly all your work looks great, and I appreciate it. I just didn't want to setup barriers to further migration. This certainly seems like it establishes a pattern we could follow easily for more migrations (and I assume that will be in subsequent PRs, so happy to merge this and continue with that).

@cap10morgan
Copy link
Member Author

@kriszyp Does 630b499 look better?

Yes. I really wasn't trying to get you to revert things either, honestly all your work looks great, and I appreciate it. I just didn't want to setup barriers to further migration. This certainly seems like it establishes a pattern we could follow easily for more migrations (and I assume that will be in subsequent PRs, so happy to merge this and continue with that).

Great! Once this lands, I'll open PRs for some of the test categories I migrated previously and so hopefully we'll have quite a few unit tests in short order. Then I can write up a migration guide and we can start trying to migrate others. Might make sense at that point to write up GitHub issues for the migration of test categories (roughly unitTests subdirs, I would imagine) and start assigning them out to the team?

@kriszyp
Copy link
Member

kriszyp commented Jan 5, 2026

That sounds great, thank you!

@kriszyp kriszyp requested review from cb1kenobi and heskew January 6, 2026 16:29
@cb1kenobi
Copy link
Contributor

I don't understand why this code was changed from TypeScript to JavaScript and ESM to CJS. This baffles me.

@cap10morgan
Copy link
Member Author

I don't understand why this code was changed from TypeScript to JavaScript and ESM to CJS. This baffles me.

We discussed this in previous PRs and engineering meetings. We decided to redo the unit test migration and just keep the unit tests as CJS for now. So this change reflects that decision.

@cb1kenobi
Copy link
Contributor

I don't understand why this code was changed from TypeScript to JavaScript and ESM to CJS. This baffles me.

We discussed this in previous PRs and engineering meetings. We decided to redo the unit test migration and just keep the unit tests as CJS for now. So this change reflects that decision.

I don't recall this being mentioned in any previous PRs or engineering meetings, otherwise I wouldn't ask. I don't understand the rationale for keeping unit tests as CJS for now. This is obviously making more work for you/us.

@cap10morgan
Copy link
Member Author

I don't understand why this code was changed from TypeScript to JavaScript and ESM to CJS. This baffles me.

We discussed this in previous PRs and engineering meetings. We decided to redo the unit test migration and just keep the unit tests as CJS for now. So this change reflects that decision.

I don't recall this being mentioned in any previous PRs or engineering meetings, otherwise I wouldn't ask. I don't understand the rationale for keeping unit tests as CJS for now. This is obviously making more work for you/us.

One example: #15 ...but the bulk of the conversation was in a Zoom meeting around then.

@cb1kenobi
Copy link
Contributor

I don't understand why this code was changed from TypeScript to JavaScript and ESM to CJS. This baffles me.

We discussed this in previous PRs and engineering meetings. We decided to redo the unit test migration and just keep the unit tests as CJS for now. So this change reflects that decision.

I don't recall this being mentioned in any previous PRs or engineering meetings, otherwise I wouldn't ask. I don't understand the rationale for keeping unit tests as CJS for now. This is obviously making more work for you/us.

One example: #15 ...but the bulk of the conversation was in a Zoom meeting around then.

Thank you for the link @cap10morgan ! Unfortunately, I do not agree with the direction of this PR. I fail to understand the reason (technical or otherwise) for abandoning TypeScript and ESM. I cannot green light this PR. Whomever made the call to abandon TS and ESM should be reviewing this PR.

@kriszyp
Copy link
Member

kriszyp commented Jan 6, 2026

I don't understand why this code was changed from TypeScript to JavaScript and ESM to CJS. This baffles me.

This PR isn't really about these unit test themselves, but about the pattern we are setting up for migrating unit tests from /harperdb. And those tests are (predominantly) CJS (these tests originate here: https://github.com/HarperFast/harperdb/tree/main/unitTests/components). That's just the fact of reality. So the change is to establish a pattern that minimizes the changes needed when we migrate/copy the tests over, so we can do that as efficiently as possible and then any changes that we want to make to unit tests are actually properly tracked in version control/commits (and has associated tickets, hopefully).

So the really is not about making or precluding any decisions about eventually migrating tests to ESM, TS, vitest, or anything else, those are still very much open possibilities. But this is about preserving our core values, that we want to do that openly, in the public, with a clear commit history. And again, the immediate priority is to get the tests migrated and working; which is actually critical for effectively and sanely modifying tests to another form, so that we can start with working/passing unit tests and then modify tests with confidence that the code itself works and any changes to unit tests can be isolated and fixed to pass unit tests. (migrating and modifying tests at the same time is asking for trouble of not know if failures are due to source code or unit test changes).

(we could keep a branch of the changes here from the .ts -> .ts with the new module specifiers, but its not that many tests, and I assume it is preserved in the commit history anyway, for future usage).

@heskew
Copy link
Member

heskew commented Jan 7, 2026

My $.02, fwiw, why don't do do both, more or less (but probably not all at once)? Pull over all of the existing unit tests into, say, test-legacy/unit/ and have clean, modern, unit tests in test/unit/ (or whatever path patterns that make sense)? Get the value in all of our existing tests and make it clear and easy to build new tests and migrate existing while having at least similar coverage to what we have now? There's maybe little harm to have the extra/duplicate coverage in the meantime? Separate branch(es) might be more difficult to coordinate and maintain - but maybe this proposal would be a real mess to to have in place... 🤔

@kriszyp
Copy link
Member

kriszyp commented Jan 8, 2026

Pull over all of the existing unit tests into, say, test-legacy/unit/ and have clean, modern, unit tests in test/unit/

This is an interesting idea, but what is the criteria for what goes in test-legacy vs test? Should everything in /harperdb go in test-legacy, even if it was written in the last month? Everything older than a year? Or stuff that mutates module records? Tests in CJS? I am not a fan of using different folders to designate different file types, that's why we have file extensions; file extensions can provide a very clear delineation if that was criteria. Part of the problem with this is that we have not defined exactly everything that will be done to upgrade or change test formats in the future. And that was a key point, we aren't going to wait for that definition, and we are not trying to define that in this PR (or even release, most likely).

Of all of these possibilities, separating out tests that mutate module records (use rewrite/stubs) actually sounds the most compelling to me, since this is the most significant issue we know of that precludes upgrading some tests to ESM/TypeStrip. I think it is our belief that generally tests that do this will be quite difficult to upgrade to TypeStrip/ESM and tests that don't will likely be relatively easy to switch. Using folders to designate tests that use module record modification (and require more extensive rewriting), would probably be helpful separate designation from tests that need a CJS->ESM upgrade, which could be designated by file extension. And it is likely that tests will be upgraded in multiple phases, so having multiple designations is probably wiser.

If we go this route, I don't think there is anything to change in this PR, these tests are already considered "modern" from the module record immutability perspective.

And I do recognize that this PR has become a bit confusing; again I think it should be understood as the pattern for how we will do initial copy/migration of tests from /harperdb, but this PR doesn't actually copy/migrate any tests from /harperdb, its diff is a modification of existing tests to show how other tests will be migrating (which isn't the clearest way to demonstrate that). And to that point, merging this PR, at least the test changes, isn't actually necessary for us to get to work on migrating other tests, AFAICT, the main key thing to merge (for other tests to follow this pattern) is the package.json changes to setup the subpath importing. And I suppose it is even feasible to copy tests and subsequently change the paths.

I'd be happy to create a separate PR for the package.json changes, if that helps.

@cap10morgan
Copy link
Member Author

cap10morgan commented Jan 12, 2026

Yeah, I think it's time to merge this and iterate forward. Some unit tests are better than no unit tests. This represents a pretty minimal change (in the grand scheme of things; the git diff of this PR actually shows more churn than there really is; this PR undoes some of the premature changes that previously landed in this repo) and is the result of much discussion, research, and pondering how best to proceed. And like Kris said, we haven't fully defined how we want unit tests to evolve in the future. But merging this allows more work to start moving forward and with greater confidence w/ automated tests, IMO.

@cap10morgan cap10morgan merged commit 6adde1c into main Jan 12, 2026
22 checks passed
@cap10morgan cap10morgan deleted the chore/unit-tests-third-times-a-charm branch January 12, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments